package com.yuanhao.securitydemo.hellocontroller;

import com.yuanhao.securitydemo.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("excel")
public class HelloController {

    /**
     * excel 文件上传
     * @param file excel文件
     * @return 返回上传的数据格式
     */
    @PostMapping("input")
    public List<List<Object>> operate(@RequestParam("file") MultipartFile file){
        //文件上传前的名称
        String fileName = file.getOriginalFilename();
        File inputFile = new File(fileName);
        OutputStream out = null;
        try{
            //获取文件流，以文件流的方式输出到新文件
            out = new FileOutputStream(inputFile);
            byte[] ss = file.getBytes();
            for(int i = 0; i < ss.length; i++){
                out.write(ss[i]);
            }
            //当前数据是读取出来的数据
            List<List<Object>> lists = ExcelUtil.readExcel(inputFile);
            if (CollectionUtils.isEmpty(lists)){
                throw new RuntimeException("读取数据失败");
            }
            return lists;
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            if (out != null){
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * excel 文件下载
     * @param objects 源数据
     */
    @PostMapping("outPut")
    public void outPut(@RequestBody List<Object> objects, HttpServletResponse response) throws Exception{
        ExcelUtil<Object> excelUtil = new ExcelUtil<>();
        //excel的表头
        String str = "";
        String[] split = str.split(",");
        HSSFWorkbook sheets = excelUtil.excelOutPut(split, "数据", objects);
        OutputStream out = null;
        if(sheets !=null){
            try {
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-Disposition","attachment; filename="+new String("导出数据".getBytes("utf-8"),"ISO8859-1")+ ".xls");
                out = response.getOutputStream();
                sheets.write(out);
            }
            catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (!Objects.isNull(out)){
                    try{
                        out.close();
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                }
                if (Objects.isNull(sheets)){
                    sheets.close();
                }
            }
        } else {
            sheets.close();
        }
    }
}
